package com.wzdigit.wms.wms.service.mid;

import com.wzdigit.wms.basic.domain.mid.JobExecStatus;
import com.wzdigit.wms.basic.domain.mid.JobSyncErrorData;
import com.wzdigit.wms.basic.mapper.core.mid.JobExecStatusMapper;
import com.wzdigit.wms.basic.mapper.core.mid.JobSyncErrorDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author borui_zhuang
 * @since 2020-05-14
 */
@Service
@Order(2)
public class SyncErrorDataService {

	@Autowired
	private JobSyncErrorDataMapper jobSyncErrorDataMapper;
	
	@Autowired
	private JobExecStatusMapper jobExecStatusMapper;
	
	public List<JobSyncErrorData> findSyncErrorData(String syncErrorType) {
		return jobSyncErrorDataMapper.selectSyncErrorDataByType(syncErrorType);
	}
	
	public void saveSyncErrorData(String syncErrorType, String code, String failedReason) {
		saveSyncErrorData(syncErrorType, code, failedReason, -1);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
	public void saveSyncErrorData(String syncErrorType, String code, String failedReason, int retryNum) {
		JobSyncErrorData record = jobSyncErrorDataMapper.getSyncErrorDataByTypeAndCode(syncErrorType, code);
		if(record != null) {
			if(retryNum < 0) {
				retryNum = record.getRetryNum() + 1;
			}
			record.setFailedReason(failedReason);
			record.setRetryNum(retryNum);
			record.setModifyUser("system");
			record.setModifyTime(new Date());
			jobSyncErrorDataMapper.updateById(record);
		} else {
			record = new JobSyncErrorData();
			record.setSyncErrorDataType(syncErrorType);
			record.setSyncErrorDataJsonobj(code);
			record.setFailedReason(failedReason);
			record.setRetryNum(1);
			record.setIsSuccess(0);
			record.setCreateUser("system");
			record.setCreateTime(new Date());
			jobSyncErrorDataMapper.insert(record);
		}
	}
	
	public int getRetryNumByTypeAndCode(String syncErrorType, String code) {
		JobSyncErrorData record = jobSyncErrorDataMapper.getSyncErrorDataByTypeAndCode(syncErrorType, code);
		if(record != null) {
			return record.getRetryNum();
		}
		return 1;
	}
	
    public int batchUpdateSuccessByIds(String syncErrorType, List<Integer> updThirdIds) {
    	return jobSyncErrorDataMapper.batchUpdateSuccessByIds(syncErrorType, updThirdIds);
    }
    
    public int updateSuccessByCode(String syncErrorType, String code) {
    	return jobSyncErrorDataMapper.updateSuccessByCode(syncErrorType, code);
    }

	public JobExecStatus getJobExecStatus(String interfaceName) {
		return jobExecStatusMapper.getJobExecStatus(interfaceName);
	}
	
	public void updateJobExecStatus(String interfaceName, Integer lastExecId) {
		JobExecStatus status = jobExecStatusMapper.getJobExecStatus(interfaceName);
		if(status != null) {
			status.setLastExecId(lastExecId);
			jobExecStatusMapper.updateById(status);
		} else {
			if(lastExecId != 0) {
				status = new JobExecStatus();
				status.setInterfaceName(interfaceName);
				status.setIsRun(1);
				status.setLastExecId(lastExecId);
				jobExecStatusMapper.insert(status);
			}
		}
	}
}
